package i2.application.checklist.entities;

import static javax.persistence.GenerationType.AUTO;
import i2.application.checklist.codes.AlerteNiveau;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name = "alerte")
public class Alerte {

	@Id
	@GeneratedValue(strategy = AUTO)
	@Column(name = "id", unique = true, nullable = false)
	private int id;
	
	@Column(name = "message", nullable = false, length = 250)
	@NotNull
	@NotEmpty
	@Size(max=255)
	private String message;
	
	@Enumerated(EnumType.STRING)
	@Column(name = "niveau", nullable = false)
	@NotNull
	private AlerteNiveau niveau = AlerteNiveau.INFO;
	
	@ManyToOne
	@JoinColumn(name = "rapport_id")
	private Rapport rapport;
	
	@OneToOne(mappedBy = "alerte", fetch = FetchType.LAZY)
	private Question question;
	
	public Alerte() {
	}
	
	public Alerte(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public AlerteNiveau getNiveau() {
		return niveau;
	}

	public void setNiveau(AlerteNiveau niveau) {
		this.niveau = niveau;
	}
	
	public void setNiveau(String niveau) {
		this.niveau = AlerteNiveau.valueOf(niveau);
	}
	
	public Rapport getRapport() {
		return rapport;
	}

	public void setRapport(Rapport rapport) {
		this.rapport = rapport;
	}
	
	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Alerte other = (Alerte) obj;
		if (id != other.id) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "Alerte [id=" + id + ", message=" + message + ", niveau="
				+ niveau + "]";
	}
	
}
